<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="快递公司" prop="courierCompany">
        <el-select v-model="queryParams.courierCompany" placeholder="请选择快递公司" clearable>
          <el-option
            v-for="dict in dict.type.express_company"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="快递单号" prop="parcelCode">
        <el-input
          v-model="queryParams.parcelCode"
          placeholder="请输入快递单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收件人姓名" prop="recipientName">
        <el-input
          v-model="queryParams.recipientName"
          placeholder="请输入收件人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收件人联系方式" prop="recipientPhone">
        <el-input
          v-model="queryParams.recipientPhone"
          placeholder="请输入收件人联系方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="快件到达时间" prop="arrivalTime">
        <el-date-picker clearable
                        v-model="queryParams.arrivalTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择快件到达时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="快件当前位置" prop="location">
        <el-input
          v-model="queryParams.location"
          placeholder="请输入快件当前位置"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否通知" prop="notificationSent">
        <el-select v-model="queryParams.notificationSent" placeholder="请选择是否通知" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="取件码" prop="pickupCode">
        <el-input
          v-model="queryParams.pickupCode"
          placeholder="请输入取件码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['system:inventory:add']"-->
<!--        >新增-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['system:inventory:edit']"-->
<!--        >修改-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['system:inventory:remove']"-->
<!--        >删除-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['system:inventory:export']"-->
<!--        >导出-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

    <el-table v-loading="loading" :data="inventoryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="快件编号，主键，自增" align="center" prop="parcelId" />-->
      <el-table-column label="快递公司" align="center" prop="courierCompany">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.express_company" :value="scope.row.courierCompany"/>
        </template>
      </el-table-column>
      <el-table-column label="快递单号" align="center" prop="parcelCode"/>
      <el-table-column label="收件人姓名" align="center" prop="recipientName"/>
      <el-table-column label="收件人联系方式" align="center" prop="recipientPhone"/>
      <el-table-column label="快件到达时间" align="center" prop="arrivalTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.arrivalTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="快件当前位置" align="center" prop="location"/>
      <el-table-column label="快件状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.parcel_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="是否通知" align="center" prop="notificationSent">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.notificationSent"/>
        </template>
      </el-table-column>
      <el-table-column label="取件码" align="center" prop="pickupCode"/>
      <el-table-column label="出库时间" align="center" prop="outboundTime"/>
      <el-table-column label="预约时间" align="center" prop="subscribeTime">
<!--        <template slot-scope="scope">-->
<!--          {{parseTime(scope.row.subscribeTime,'yyyy-MM-dd HH:mm:ss')}}-->
<!--        </template>-->
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['system:inventory:edit']"-->
<!--          >修改-->
<!--          </el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['system:inventory:remove']"-->
<!--          >删除-->
<!--          </el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="chuku(scope.row)"
            v-if="scope.row.status==1"
            v-hasPermi="['system:inventory:edit']"
          >出库
          </el-button>
          <el-button
            v-if="scope.row.status==1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="yuyue(scope.row)"
            v-hasPermi="['system:inventory:edit']"
          >预约
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改快递入库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="快递公司" prop="courierCompany">
          <el-select v-model="form.courierCompany" placeholder="请选择快递公司" disabled>
            <el-option disabled
              v-for="dict in dict.type.express_company"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="快递单号" prop="parcelCode">
          <el-input v-model="form.parcelCode" placeholder="请输入快递单号" readonly/>
        </el-form-item>
        <el-form-item label="收件人姓名" prop="recipientName">
          <el-input v-model="form.recipientName" placeholder="请输入收件人姓名" readonly/>
        </el-form-item>
        <el-form-item label="收件人联系方式" prop="recipientPhone">
          <el-input v-model="form.recipientPhone" placeholder="请输入收件人联系方式" readonly/>
        </el-form-item>
        <el-form-item label="快件到达时间" prop="arrivalTime">
          <el-date-picker clearable readonly
                          v-model="form.arrivalTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择快件到达时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="快件当前位置" prop="location">
          <el-input v-model="form.location" placeholder="请输入快件当前位置" readonly/>
        </el-form-item>
        <el-form-item label="取件码" prop="pickupCode">
          <el-input v-model="form.pickupCode" placeholder="请输入取件码" readonly/>
        </el-form-item>
        <el-form-item label="出库时间"  prop="outboundTime" >
          <el-input v-model="form.outboundTime" placeholder="请输入出库时间" readonly/>
        </el-form-item>
        <el-form-item label="预约时间" prop="subscribeTime">
          <el-date-picker
            v-model="form.subscribeTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择预约时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listInventory, getInventory, delInventory, addInventory, updateInventory} from "@/api/system/outbound";
export default {
  name: "Outbound",
  dicts: ['sys_yes_no', 'parcel_status', 'express_company'],
  data() {
    const validateChineseName = (rule, value, callback) => {
      const chineseNameRegex = /^[\u4e00-\u9fa5]{2,5}$/;
      if (!value) {
        return callback(new Error('收件人姓名不能为空'));
      } else if (!chineseNameRegex.test(value)) {
        return callback(new Error('收件人姓名必须是2到5个简体中文字符'));
      } else {
        return callback();
      }
    };
    const validateParcelCode = (rule, value, callback) => {
      const parcelCodeRegex = /^[a-zA-Z]{1,3}[0-9]{5,16}$/;
      if (!value) {
        return callback(new Error('快递单号不能为空'));
      } else if (!parcelCodeRegex.test(value)) {
        return callback(new Error('快递单号必须是1到3位英文字母和5到16位数字的组合'));
      } else {
        return callback();
      }
    };
    const validatePickupCode = (rule, value, callback) => {
      const pickupCodeRegex = /^[0-9_\-]{4,8}$/;
      if (!value) {
        return callback(new Error('取件码不能为空'));
      } else if (!pickupCodeRegex.test(value)) {
        return callback(new Error('取件码必须是4到8位数字、短横线、下划线的组合'));
      } else {
        return callback();
      }
    };
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 快递入库表格数据
      inventoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        courierCompany: null,
        recipientName: null,
        recipientPhone: null,
        arrivalTime: null,
        location: null,
        status: null,
        notificationSent: null,
        pickupCode: null,
        selectType: 1
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        courierCompany: [
          {required: true, message: "快递公司不能为空", trigger: "change"},
          // {validator: validateChineseName, trigger: 'blur'}
        ],
        recipientName: [
          {required: true, message: "收件人姓名不能为空", trigger: "blur"}
        ],
        recipientPhone: [
          {required: true, message: "收件人联系方式不能为空", trigger: "blur"}
        ],
        arrivalTime: [
          {required: true, message: "快件到达时间不能为空", trigger: "blur"}
        ],
        location: [
          {required: true, message: "快件当前位置不能为空", trigger: "blur"}
        ],
        parcelCode: [
          {required: true, message: "快递单号不能为空", trigger: "blur"},
          {validator: validateParcelCode, trigger: 'blur'}
        ],
        pickupCode: [
          {required: true, message: '取件码不能为空', trigger: 'blur'},
          {validator: validatePickupCode, trigger: 'blur'}
        ],
        subscribeTime: [
          {required: true, message: '预约时间不能为空', trigger: 'blur'},
        ]

      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    yuyue(row){
      this.reset();
      const parcelId = row.parcelId || this.ids
      getInventory(parcelId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "预约";
      });

    },
    // 出库
    chuku(row){
      this.reset();
      const parcelId = row.parcelId || this.ids
      this.form.status = 2 // 状态改为已出库
      this.form.parcelId = parcelId // 状态改为已出库
      // `this.$moment()` 输出当前时间的moment对象
      this.form.outboundTime = this.$moment().format('YYYY-MM-DD HH:mm:ss')

      console.log(this.form)
      updateInventory(this.form).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.getList();
      });
    },
    /** 查询快递入库列表 */
    getList() {
      this.loading = true;
      listInventory(this.queryParams).then(response => {
        this.inventoryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        parcelId: null,
        courierCompany: null,
        recipientName: null,
        recipientPhone: null,
        arrivalTime: null,
        location: null,
        status: null,
        notificationSent: null,
        pickupCode: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.parcelId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加快递入库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const parcelId = row.parcelId || this.ids
      getInventory(parcelId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改快递入库";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.parcelId != null) {
            updateInventory(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInventory(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const parcelIds = row.parcelId || this.ids;
      this.$modal.confirm('是否确认删除快递入库编号为"' + parcelIds + '"的数据项？').then(function () {
        return delInventory(parcelIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/inventory/export', {
        ...this.queryParams
      }, `inventory_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
